<template>
    <div class="light-Ct">
        <!-- 横向Dom -->
        <div class="item-Cd item-Cd-Dom"
            :class="{ 'item-Cd-Dom-line': parentChildrenLength && parentChildrenLength > 1 || (parentChildrenLength && parentChildrenLength > 2 && item.children && item.children.length > 2) }">

            <div class="item-circ-wrapper">
            
                <!-- <div class="input-row top-row">
                    <span class="input-span">+</span>
                    <input type="text" class="node-input" placeholder="Top" />
                </div> -->
                
                <div class="input-row middle-row">
                    <!-- <div class="left-row">
                        <span class="input-span">+</span>
                        <input type="text" class="node-input" placeholder="Left" />
                    </div> -->
                    <el-popover placement="top-start" :width="200" trigger="hover" v-model:visible="popoverVisible">
                        <template #reference>
                            <div class="item-circ">
                                <div class="light-circle" :class="{
                                    'light-green': item.nodeState == 1,
                                    'light-red': item.nodeState == 0,
                                    'light-orange': item.nodeState == 2,
                                }">
                                    <span class="circleTit"> {{ item.percent }} </span>
                                    <transition name="fade" v-if="item.firstLevel !== true">
                                        <div v-if="popoverVisible" class="opers operRD"
                                            @click="addBrotherItem(index, item)">+
                                        </div>
                                    </transition>
                                    <transition name="fade">
                                        <div v-if="popoverVisible" class="opers operXD"
                                            @click="addChildNestedItem(index, item)">+</div>
                                    </transition>
                                </div>
                            </div>
                        </template>
                        <OperationButtons :index="index" :item="item" @add-brother-item="addBrotherItem"
                            @add-child-item="addChildItem" @edit-item="testIntem" @delete-item="deleteItem"
                            @seeNode-Detail="seeNodeDetail" @toggle-expand="expandCollapse" />
                    </el-popover>
                    <!-- <div class="right-row">
                        <span class="input-span">+</span>
                        <input type="text" class="node-input" placeholder="Right" />
                    </div> -->
                </div>

                <div class="input-row bottom-row">
                    <div v-if="!isEditing" @click="startEdit()" class="pointers"
                        style="display: inline-block;line-height: 26px;">
                        {{ item.name }}</div>
                    <el-input v-else v-model="editedName" @blur="finishEdit" @keyup.enter="finishEdit" ref="editInput"
                        type="text" class="node-input" placeholder="Bottom" />
                </div>
            </div>

            <el-tag v-if="item.children.length > 0" class="item-chNum" type="info" @click="expandCollapse(index, item)">
                {{ item.expanded ? "-" : item.children.length }}
            </el-tag>

            <!-- <div>id: {{ item.id }} </div>
            <div>父级id: {{ item.parentId }} </div> -->
            <!-- <div>nodeState：{{ item.nodeState }}</div> -->
            <!-- <div>父{{ parentChildrenLength }} 子{{ item.children && item.children.length }}</div> -->
        </div>
        <div style="display: flex;align-items: center;justify-content: center;">
            <div v-if="item.expanded && item.children && item.children.length"
                :class="item.children && item.children.length ? 'childItem childItemSon' : 'childItem'">
                <!-- :style="parentChildrenLength && parentChildrenLength == 1 || item.children && item.children.length == 0 ? 'display: block;' : ''" -->
                <RecursiveItmSX v-for="(child, childIndex) in item.children" :key="childIndex" :item="child"
                    :index="childIndex" @editItem="editItem" @addBrotherItem="addBrotherItem"
                    @addChildItem="addChildItem" @addChildNestedItem="addChildNestedItem" @deleteItem="deleteItem"
                    @testIntem="testIntem" @seeNodeDetail="seeNodeDetail" @expandCollapse="expandCollapse"
                    :parentChildrenLength="item.children.length" />
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, defineProps, defineEmits, onUnmounted, nextTick, watch } from 'vue';
import RecursiveItmSX from './RecursiveItemSX';
import OperationButtons from './OperationButtons.vue';

const props = defineProps({
    item: {
        type: Object,
        required: true
    },
    index: {
        type: Number,
        required: true
    },
    parentChildrenLength: { // 新增 prop
        type: Number,
        default: 0
    }
});

const emit = defineEmits(['testIntem', 'editItem', 'addBrotherItem', 'addChildItem', 'deleteItem', 'seeNodeDetail', 'expandCollapse', "addChildNestedItem"]);

const isEditing = ref(false);
const editedName = ref(props.item.name);
const editInput = ref(null);
const popoverVisible = ref(false);

watch(() => props.item, (newItem) => {
    if (newItem) {
        editedName.value = newItem.name === 'A' ? '' : newItem.name;
    }
},
    { immediate: true }
);
const startEdit = () => {
    isEditing.value = true;
    nextTick(() => {
        if (isEditing.value && editInput.value) {
            editInput.value.focus();
        }
    });
};
const finishEdit = () => {
    isEditing.value = false;
    if (!editedName.value) {
        return; // ElMessage.error("请输入节点名称");
    }
    if (editedName.value !== props.item.name) {
        emit('editItem', props.index, { ...props.item, name: editedName.value });
    }
};

const testIntem = (index, item) => {
    emit('testIntem', index, item);
};

const editItem = (index, item) => {
    emit('editItem', index, item);
};

const addBrotherItem = (index, item) => {
    emit('addBrotherItem', index, item);
};

const addChildItem = (index, item) => {
    emit('addChildItem', index, item);
};

const deleteItem = (index, item) => {
    emit('deleteItem', index, item);
};

const seeNodeDetail = (index, item) => {
    emit('seeNodeDetail', index, item)
};

const expandCollapse = (index, item) => {
    emit('expandCollapse', index, item);
};

const addChildNestedItem = (index, item) => {
    emit('addChildNestedItem', index, item);
};

onMounted(() => {

});

onUnmounted(() => {
    editInput.value = null;
});

</script>

<style lang="scss" scoped>
.el-input__inner {
    ime-mode: auto;
    /* 确保输入法模式正确 */
}

.pointers {
    cursor: pointer;
}

// 横向样式
.light-item {
    .light-Ct {
        .item-Cd {
            // padding: 0px 10px;
            box-sizing: border-box;
            position: relative;
            // min-width: 76px;
            font-size: var(--global-font-size);

            .item-circ {
                display: inline-block;
                // margin: 20px;

                .light-circle {
                    width: 15px;
                    height: 15px;
                    border-radius: 50%;
                    background-color: gray;
                    cursor: pointer;
                    line-height: 30px;
                    margin: auto;
                    color: black;
                    z-index: 99;
                    position: relative;

                    &::after {
                        content: "";
                        position: absolute;
                        left: 50%;
                        bottom: -10px;
                        width: 1px;
                        height: 10px;
                        background-color: black;
                    }

                    &.light-green {
                        background-color: green;
                    }

                    &.light-red {
                        background-color: red;
                    }

                    &.light-orange {
                        background-color: orange;
                    }

                    &.light-gray {
                        background-color: gray;
                    }

                    .circleTit {
                        position: absolute;
                        top: -7px;
                        left: 20px;
                        font-weight: bold;
                    }

                    .opers {
                        width: 12px;
                        height: 12px;
                        border: 1px solid;
                        border-radius: 2px;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        background: #fff;
                        z-index: 99;
                        color: var(--global-link-color);

                        &.operRD {
                            position: absolute;
                            right: -15px;
                            top: 0px;
                        }

                        &.operXD {
                            position: absolute;
                            bottom: -15px;
                            right: 0px;

                        }
                    }
                }

            }

            .item-btn {
                a {
                    color: var(--el-color-primary);
                    cursor: pointer;
                }

                span {
                    color: #eee;
                    margin: 0px 4px;
                }
            }

            .item-name {
                margin: 10px 0px;
                color: var(--global-font-color);
                font-size: var(--global-font-size);

                // display: inline-block;
                &.item-name-mar {
                    margin: 24px 0px 10px;
                }
            }

            .operation {
                a {
                    color: var(--global-link-color);
                    cursor: pointer;
                }

                span {
                    color: #eee;
                    // margin: 0px 4px;
                }

                &.operation2 {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    margin: 10px 0px;
                }
            }

            .item-chNum {
                font-size: 12px;
                color: var(--global-font-color);
                border-radius: 50%;
                padding: 0px;
                min-width: 16px;
                height: 16px;
                position: relative;
                cursor: pointer;

                // &::before {
                //     content: "";
                //     position: absolute;
                //     position: absolute;
                //     top: -10px;
                //     left: 50%;
                //     width: 1px;
                //     height: 10px;
                //     background-color: rgb(43, 38, 38);
                // }
            }

        }
    }

    .childItem {
        position: relative;
        display: flex;

        .item-Cd-Dom-line::after {
            content: "";
            position: absolute;
            left: 0px;
            top: 12px;
            width: 100%;
            height: 1px;
            background-color: rgb(43, 38, 38);
        }

        >:first-child>.item-Cd-Dom-line::after {
            content: "";
            position: absolute;
            left: 50% !important;
            top: 12px;
            width: 50%;
            height: 1px;
            background-color: black;
        }

        >:last-child>.item-Cd-Dom-line::after {
            content: "";
            position: absolute;
            left: 0px !important;
            top: 12px;
            width: 50%;
            height: 1px;
            background-color: black;
        }

    }

    .childItemSon>.light-Ct::before {
        content: "";
        position: absolute;
        left: 50%;
        top: 0px;
        width: 1px;
        height: 12px;
        background-color: black;
    }

}


.item-circ-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    min-width: 76px;
    .input-row {
        display: flex;
        justify-content: center;
        align-items: center;
        min-height: 26px;

        // width: 100%;
        // margin: 5px 0; // 调整输入框与节点之间的距离
        .input-span {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background: darkgrey;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 12px;
            color: #fff;
            z-index: 99;
            cursor: pointer;
        }
    }

    .top-row,
    .bottom-row {
        justify-content: center;
    }

    .middle-row {
        display: flex;
        justify-content: space-between;
        align-items: center;

        .item-circ {
            margin: 0 10px; // 节点和左右输入框之间的间距
        }
    }

    .node-input {
        width: 80px;
        height: 26px;
        font-size: 14px;
        text-align: center;
        border: 1px solid #ccc;
        border-radius: 4px;
        background-color: #f9f9f9;
        box-sizing: border-box;
    }
}


.item-info {
    text-align: left;
    line-height: 20px;
    color: var(--global-font-color);
    font-size: var(--global-font-size);

    p {
        margin: 0px;
    }
}

.light-wrapHXXS {

    .pointers,
    .circleTit,
    .el-input,.item-chNum {
        transform: scaleY(-1);
        writing-mode: horizontal-tb;
    }
}
</style>
